package com.cyz.web.app.proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class JdkProxyClient {

    public static void main(String[] args) {
        String username = "caiya";

        IBuyer buyer = new Buyer(username);

        IBuyer proxy = (IBuyer) Proxy.newProxyInstance(buyer.getClass().getClassLoader(), buyer.getClass().getInterfaces(), (proxy1, method, args1) -> {
            Object obj = null;

            if ("buyTicket".equals(method.getName())) {
                System.out.println("准备定时任务，开始抢票");
                obj = method.invoke(buyer, args1);
                System.out.println("抢票成功, 短信通知用户");
            } else {
                System.out.println(args1);
                obj = method.invoke(buyer, args1);
            }
            return obj;
        });


        proxy.login(username, "123456");


        proxy.buyTicket();


    }


}
